//
//  ShoppingCartViewController.m
//  LoveFreshBeen
//
//  Created by 魏根 on 2016/10/15.
//  Copyright © 2016年 魏根. All rights reserved.
//
#import "ShopCartGoodsListTool.h"
#import "ShoppingCartViewController.h"
#import "ReceiverInfoCell.h"
#import "CartBottomView.h"
#import "PayTableViewController.h"
#import "RemarkCell.h"
#import "HMHomeFirstModel.h"
#import "OrderedGoodsCell.h"
#import "emptyCartView.h"
@interface ShoppingCartViewController ()<CartBottomViewDelegate,UITableViewDataSource,UITableViewDelegate,OrderedGoodsCellDelegate,emptyCartViewdelegate>
@property(nonatomic,weak) CartBottomView* bottomView;
@property(nonatomic,weak) UITableView* tableView;
@property(nonatomic,strong)NSArray* goodsArray;
@property(nonatomic,weak) emptyCartView* emptyView;
@property(nonatomic,assign)BOOL isAnimating;


@end

@implementation ShoppingCartViewController
-(instancetype)init{
    if (self =[super init]) {
        [self registerNotificationObserver];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
}

-(void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)setUpUI {
    UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
    self.navigationItem.title = @"购物车";
    UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
    [btn setBackgroundImage:[UIImage imageNamed:@"v2_goback"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [self.tableView registerClass:[ReceiverInfoCell class] forCellReuseIdentifier:@"receiver"];
    CartBottomView* bottomView = [[[NSBundle mainBundle] loadNibNamed:@"CartBottomView" owner:nil options:nil] lastObject];
    self.bottomView = bottomView;
    self.bottomView.modelArray = self.goodsArray;
    bottomView.delegate = self;
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@49);
    }];
    self.emptyView.frame = self.view.bounds;
    if (self.goodsArray.count > 0) {
        [self.view bringSubviewToFront:self.tableView];
        [self.view bringSubviewToFront:self.bottomView];
    }else{
        [self.view bringSubviewToFront:self.emptyView];
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    else{
        return 3 + self.goodsArray.count;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* cell;
    if (indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"receiver" forIndexPath:indexPath];
    }
    else {
        if (indexPath.row == 0) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"SuperMarketTipCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        else if(indexPath.row == 1){
            cell = [[[NSBundle mainBundle]loadNibNamed:@"receiveTimeCell" owner:nil options:nil] lastObject];
            
        }else if(indexPath.row == 2){
            RemarkCell* markCell = [[[NSBundle mainBundle]loadNibNamed:@"RemarkCell" owner:nil options:nil] lastObject];
            cell = markCell;
        }
        else{
            OrderedGoodsCell* orderCell = [[[NSBundle mainBundle]loadNibNamed:@"OrderedGoodsCell" owner:nil options:nil] lastObject];
            HMHomeFirstModel* model = self.goodsArray[indexPath.row-3];
            orderCell.model = model;
            orderCell.delegate = self;
            cell = orderCell;
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat rowHeight = 100;
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            rowHeight = 65;
        }
        else{
            rowHeight = 40;
        }
        
    }
    return  rowHeight;
}
#pragma mark--底部工具视图代理方法
-(void)bottomView:(CartBottomView *)bottomView didClickFinishBtn:(UIButton *)btn{
    PayTableViewController * payView = [[PayTableViewController alloc] init];
    [self.navigationController pushViewController:payView animated:YES];
}
#pragma mark--通知相关
-(void)registerNotificationObserver{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveGoodsChangeNotification:) name:KChangeGoodCountNoti object:nil];
}
-(void)didReceiveGoodsChangeNotification:(NSNotification*)noti{

    NSInteger badgeValueCount = 0;
    for (HMHomeFirstModel* model in self.goodsArray) {
        badgeValueCount += model.goodsCount;
    }
    if (badgeValueCount == 0) {
        self.tabBarItem.badgeValue = nil;
    }else
    {
        self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld",(long)badgeValueCount];
    }
    
    
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)goodsCountsEqualToZero:(HMHomeFirstModel*)model{
    [[ShopCartGoodsListTool sharedGoodsListTool] reduceGoodsFromShopCartGoodsList:model];
    [self.tableView reloadData];
    if (self.goodsArray.count == 0) {
        [self.view bringSubviewToFront:self.emptyView];
    }
    
}
-(void)emptyCartView:(emptyCartView *)emptyCart hangAroundBtnDidClick:(UIButton *)btn{
    [self back];
}
#pragma mark--懒加载
-(NSArray *)goodsArray{
    if (_goodsArray == nil) {
        _goodsArray = [ShopCartGoodsListTool sharedGoodsListTool].goodsList;
    }
    return _goodsArray;
}

-(emptyCartView *)emptyView{
    if (!_emptyView) {
        emptyCartView* emptyView = [[[NSBundle mainBundle] loadNibNamed:@"emptyCartView" owner:nil options:nil] lastObject];
        _emptyView = emptyView;
        emptyView.delegate = self;
        [self.view addSubview:emptyView];
        emptyView.frame = self.view.bounds;
    }
    return _emptyView;
}






@end
